package com.das.mechanic_base.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;

import razerdp.basepopup.BasePopupWindow;
import razerdp.util.animation.AlphaConfig;
import razerdp.util.animation.AnimationHelper;

public class X3TaskInputShortWindow extends BasePopupWindow implements TextWatcher, View.OnClickListener{

    private EditText et_input;
    private TextView tv_num;
    private Button btn_cancel;
    private Button btn_create;

    private Context mContext;

    public X3TaskInputShortWindow(Context context) {
        super(context);
        this.mContext = context;
    }

    public X3TaskInputShortWindow(Dialog dialog, Context mContext) {
        super(dialog);
        this.mContext = mContext;
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.x3_create_task_short_item);
    }

    @Override
    public void onViewCreated(@NonNull View contentView) {
        et_input = contentView.findViewById(R.id.et_input);
        tv_num = contentView.findViewById(R.id.tv_num);
        btn_cancel = contentView.findViewById(R.id.btn_cancel);
        btn_create = contentView.findViewById(R.id.btn_create);

        et_input.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
        btn_create.setOnClickListener(this);

        et_input.addTextChangedListener(this);

    }

    @Override
    public void onDismiss() {
        if (et_input != null) {
            et_input.setText("");
        }
        super.onDismiss();
    }

    @Override
    protected Animation onCreateShowAnimation() {
        return AnimationHelper.asAnimation()
                .withAlpha(AlphaConfig.IN.duration(200))
                .toShow();
    }


    @Override
    protected Animation onCreateDismissAnimation() {
        return AnimationHelper.asAnimation()
                .withAlpha(AlphaConfig.OUT.duration(200))
                .toDismiss();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String content = s.toString();
        if (X3StringUtils.isEmpty(content)) {
            tv_num.setText("0/50");
        }else {
            tv_num.setText(content.length() + "/50");
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_cancel) {
            dismiss();
        } else if (id == R.id.btn_create) {// 创建或者修改待办事项
            if (!X3Utils.isFastClick()) {
                return;
            }
            String content = et_input.getText().toString();
            if (X3StringUtils.isEmpty(content)) {
                return;
            }
            if (iOnGetTaskShort != null) {
                iOnGetTaskShort.iOnGetTaskShort(content);
            }
            dismiss();
        }
    }

    IOnGetTaskShort iOnGetTaskShort;

    public void setiOnGetTaskShort(IOnGetTaskShort iOnGetTaskShort) {
        this.iOnGetTaskShort = iOnGetTaskShort;
    }

    public interface IOnGetTaskShort {
        void iOnGetTaskShort(String content);
    }

}
